﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Reflection;
using System.Text;
using TaoLeH.Helper.Config;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.OpenApi.Models;

namespace TaoLeH.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "TaoLeH.WebApi", Version = "v1" });

                var basePath = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationBasePath; // 获取到应用程序的根路径
                options.IncludeXmlComments(basePath + "\\TaoLeH.WebApi.xml");  //是需要设置 XML 注释文件的完整路径
            });

            //注册GB2312编码格式
            //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            ServerConfigs config = new ServerConfigs();
            //读取配置信息
            Configuration.Bind(config);
            //配置文件信息存放于内存中
            ApiConfig.ServerConfigs = config;
            
            //注册业务处理服务
            RegServices();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //获取客户端ip
            app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });

            app.UseMvc();

            app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("v1/swagger.json", "TaoLeH.WebApi");
            });
        }

        /// <summary>
        /// 注册业务处理服务
        /// 作者：leev
        /// 时间：2017-12-28
        /// </summary>
        public void RegServices()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type[] typeList = assembly.GetTypes();

            for (int i = 0; i != typeList.Length; i++)
            {
                if (typeList[i].Name.EndsWith("Service") && typeList[i].Name != "ApiService")
                {
                    string typeName = typeList[i].FullName;
                    Type t = Type.GetType(typeName);
                    Activator.CreateInstance(t);
                }
            }
        }
    }
}
